#include<iostream>
#include<cstring>
using namespace std;
const int N = 510;
int n, m, k;
char s[N][N];
int f[2][N][1010];
int main()
{
	int t;
	cin >> t;
	while (t--) {
		cin >> n >> m >> k;
		//memset(f, 0, sizeof(f));
		//memset(s, ' ', sizeof(s));
		for (int j = 1; j <= m; j++) {
			for (int z = 0; z <= k; z++) {
				 f[1][j][z] = 0;
				 f[0][j][z] = 0;
			}
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				cin >> s[i][j];
			}
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				for (int z = 0; z <= k; z++) {
					if (i & 1) {
						if (s[i][j] == '?') {
							if (!z)f[1][j][z] = max(f[0][j][z], f[1][j - 1][z]);
							else {
								f[1][j][z] = max(f[1][j][z], max(f[0][j][z - 1], f[1][j - 1][z - 1]) + 1);
								f[1][j][z] = max(f[1][j][z], max(f[0][j][z], f[1][j - 1][z]));
							}
						}
						else {	
							f[1][j][z] = max(f[0][j][z], f[1][j - 1][z]) + (s[i][j] == '1');
						}
					}
					else {
						if (s[i][j] == '?') {
							if (!z)f[0][j][z] = max(f[1][j][z], f[0][j - 1][z]);
							else {
								f[0][j][z] = max(f[0][j][z], max(f[1][j][z - 1], f[0][j - 1][z - 1]) + 1);
								f[0][j][z] = max(f[0][j][z], max(f[1][j][z], f[0][j - 1][z]));
							}
						}
						else {
							f[0][j][z] = max(f[1][j][z], f[0][j - 1][z]) + (s[i][j] == '1');
						}
					}
				}
			}
		}
		if (n & 1) {
			int ans = 0;
			for (int i = 0; i <= k; i++) {
				ans = max(ans, f[1][m][k]);
			}
			cout << ans << endl;
		}
		else {
			int ans = 0;
			for (int i = 0; i <= k; i++) {
				ans = max(ans, f[0][m][k]);
			}
			cout << ans << endl;
		}
	}
	return 0;
}